package com.example.freemarkerDemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.freemarkerDemo.domain.User;
import com.example.freemarkerDemo.domain.bo.UserBo;
import com.example.freemarkerDemo.domain.vo.UserVo;
import com.example.freemarkerDemo.mapper.UserMapper;
import com.example.freemarkerDemo.service.UserService;
import com.example.freemarkerDemo.utils.MapstructUtils;
import com.example.freemarkerDemo.utils.StringUtils;
import com.example.freemarkerDemo.utils.web.core.page.PageQuery;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @version 1.0
 * @author: 枫玥筱筱
 * @date: 2024/3/27 17:59
 * 描述：
 */
@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {
    //private final UserMapper userMapper;
    @Resource
    private UserMapper userMapper;

    /**
     * 查询列表
     * @return 集合
     */
    @Override
    public List<UserVo> queryPageList(UserBo userBo, PageQuery pageQuery) {
       /* LambdaQueryWrapper<User> user = buildQueryWrapper(userBo);
        Page<UserVo> result = userMapper.selectVoPage(pageQuery.build(), user);*/
       /* userList.forEach(item->{
            item.setDataTime(String.valueOf(item.getCreateTime()));
        });*/
        LambdaQueryWrapper<User> user = buildQueryWrapper(userBo);
        return userMapper.selectVoList(user);
    }

    private LambdaQueryWrapper<User> buildQueryWrapper(UserBo bo) {
        LambdaQueryWrapper<User> lqw = Wrappers.lambdaQuery();
        lqw.select(User::getId, User::getName, User::getEmail, User::getAge);
        lqw.eq(StringUtils.isNotEmpty(bo.getName()), User::getName, bo.getName());
        return lqw;
    }

    /**
     * 根据id查询
     * @param id 用户id
     * @return 用户对象
     */
    @Override
    public UserVo selectUserVoById(Long id) {
        return userMapper.selectVoById(id);
    }

    /**
     * 新增用户信息
     * @param userBo 用户对象
     * @return 结果
     */
    @Override
    public Boolean insertUser(UserBo userBo) {
        User user = MapstructUtils.convert(userBo, User.class);
        return userMapper.insert(user) > 0;
    }

    /**
     * 更新用户信息
     * @param userBo 用户信息
     * @return 结果
     */
    @Override
    public Boolean updateUser(UserBo userBo) {
        User user = MapstructUtils.convert(userBo, User.class);
        return userMapper.updateById(user) > 0;
    }

    @Override
    public Boolean deleteUserById(Long id) {
        return userMapper.deleteById(id) > 0;
    }
}
